package com.study.redis.user.controller;

import com.study.redis.user.domain.User;
import com.study.redis.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by hegy on 2018/6/4.
 */
@Controller
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private UserService userService;

    @GetMapping
    public String login(){
        return "login/login";
    }

    @PostMapping("index")
    public String index(HttpServletRequest request, HttpServletResponse response, User user){
        HttpSession session = request.getSession();
        User returnUser = userService.checkUser(user);
        if (returnUser != null) {
            session.setAttribute("userId", returnUser.getId());
            return "article/article_list";
        } else {
            return "login/login";
        }
    }

    @GetMapping("register")
    public String register(){
        return "login/register";
    }
}
